package com.yxy.utils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @Author 杨芯叶
 * @Create 2025-07-31-11:25
 * @Message
 **/
public class ThreadLocalDataSources {

    private static final ThreadLocal<Connection> connections = new ThreadLocal<>();

    public static void set() {
        Connection conn = DatabasePollUtil.connect();
        connections.set(conn);
    }

    public static Connection get() {
        return connections.get();
    }

    public static void remove() {
        connections.remove();
    }

    public static void close() {
        try {
            get().close();
            remove();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void setAutoCommit(boolean isAuto) {
        try {
            get().setAutoCommit(isAuto);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void commit() {
        try {
            get().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void rollback() {
        try {
            get().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
